﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using DataLayer;
using DevExpress.XtraEditors;

namespace SalesReport
{
    public partial class Form1 : XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //dt = Factory.GetFactory.ExecuteDataTable("SELECT     CUSTOMERS.name AS [Cari Ünvanı], cast(SUM(ORDER_DETAILS.total) as decimal(16,2)) AS Tutar FROM ORDER_DETAILS INNER JOIN CUSTOMERS ON ORDER_DETAILS.customer_code = CUSTOMERS.code WHERE     (ORDER_DETAILS.customer_code LIKE '%') GROUP BY CUSTOMERS.name order by SUM(ORDER_DETAILS.total) desc");
            //dt2 = Factory.GetFactory.ExecuteDataTable("SELECT     CUSTOMERS.name AS [Cari Ünvanı], cast(SUM(ORDER_DETAILS.total) as decimal(16,2)) AS Tutar FROM         ORDER_DETAILS INNER JOIN CUSTOMERS ON ORDER_DETAILS.supplier = CUSTOMERS.code WHERE     (ORDER_DETAILS.supplier LIKE '%') GROUP BY CUSTOMERS.name ORDER BY Tutar DESC");

            //chartEx1.AddArea("chartArea");
            //chartEx1.AddSeries("Ciro", "chartArea", "Cari Ünvanı", ChartValueType.String, "Tutar", ChartValueType.Double, SeriesChartType.Column);
            //chartEx1.LegendVisible = false;
            //chartEx1.DataSource = dt;
            //chartEx1.DataBind();
            //chartEx1.ChartType = ChartControl.ChartEx.ChartTypes.Style2;
            //chartEx1.InitChart();

            //chartEx2.AddArea("chartArea");
            //chartEx2.AddSeries("Ciro", "chartArea", "Cari Ünvanı", ChartValueType.String, "Tutar", ChartValueType.Double, SeriesChartType.Column);
            //chartEx2.LegendVisible = false;
            //chartEx2.DataSource = dt2;
            //chartEx2.DataBind();
            //chartEx2.ChartType = ChartControl.ChartEx.ChartTypes.Style2;
            //chartEx2.InitChart();

            GridDataRefresh();

        }

        private void GridDataRefresh()
        {
            

            DataTable dt = Factory.GetFactory.ExecuteDataTable("SELECT     CUSTOMERS.name AS [Cari Ünvanı], cast(SUM(ORDER_DETAILS.total) as decimal(16,2)) AS Tutar FROM ORDER_DETAILS WITH (NOLOCK) INNER JOIN CUSTOMERS ON ORDER_DETAILS.customer_code = CUSTOMERS.code WHERE     (ORDER_DETAILS.customer_code LIKE '%') GROUP BY CUSTOMERS.name order by SUM(ORDER_DETAILS.total) desc");
            DataTable dt2 = Factory.GetFactory.ExecuteDataTable("SELECT     CUSTOMERS.name AS [Cari Ünvanı], cast(SUM(ORDER_DETAILS.total) as decimal(16,2)) AS Tutar FROM         ORDER_DETAILS WITH (NOLOCK) INNER JOIN CUSTOMERS ON ORDER_DETAILS.supplier = CUSTOMERS.code WHERE     (ORDER_DETAILS.supplier LIKE '%') GROUP BY CUSTOMERS.name ORDER BY Tutar DESC");

            gridView1.Columns.Clear();
            gridView2.Columns.Clear();

            gridControl1.DataSource = dt;
            gridControl2.DataSource = dt2;

            gridView1.Columns[1].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            gridView1.Columns[1].DisplayFormat.FormatString = "n2";

            gridView2.Columns[1].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            gridView2.Columns[1].DisplayFormat.FormatString = "n2";

            decimal[] toplam = gridtoplamlari();

            label1.Text = "Müşteriler   : " + toplam[0].ToString("#,##0.00") + " TL";
            label2.Text = "Tedarikçiler : " + toplam[1].ToString("#,##0.00") + " TL";
            label1.Refresh();
            label2.Refresh();
        }

        private void chartEx1_DoubleClick(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private decimal[] gridtoplamlari()
        {
            decimal[] sonuc = new decimal[2];

            for (int i = 0; i < gridView1.RowCount; i++)
            {
                sonuc[0] += Convert.ToDecimal(gridView1.GetRowCellValue(i, gridView1.Columns[1]));
            }

            for (int i = 0; i < gridView2.RowCount; i++)
            {
                sonuc[1] += Convert.ToDecimal(gridView2.GetRowCellValue(i, gridView2.Columns[1]));
            }

            return sonuc;
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            //dt = Factory.GetFactory.ExecuteDataTable("SELECT     CUSTOMERS.name AS [Cari Ünvanı], SUM(ORDER_DETAILS.total) AS Tutar FROM ORDER_DETAILS INNER JOIN CUSTOMERS ON ORDER_DETAILS.customer_code = CUSTOMERS.code WHERE     (ORDER_DETAILS.customer_code LIKE '%') GROUP BY CUSTOMERS.name order by SUM(ORDER_DETAILS.total) desc");
            //dt2 = Factory.GetFactory.ExecuteDataTable("SELECT     CUSTOMERS.name AS [Cari Ünvanı], SUM(ORDER_DETAILS.total) AS Tutar FROM         ORDER_DETAILS INNER JOIN CUSTOMERS ON ORDER_DETAILS.supplier = CUSTOMERS.code WHERE     (ORDER_DETAILS.supplier LIKE '%') GROUP BY CUSTOMERS.name ORDER BY Tutar DESC");
            ////chartEx1.DataSource = dt;
            ////chartEx1.DataBind();
            ////chartEx2.DataSource = dt2;
            ////chartEx2.DataBind();

            //decimal[] toplam = gridtoplamlari();

            //label1.Text = "Tedarikçiler  : " + toplam[0].ToString("#,##0.00") + " TL";
            //label2.Text = "Müşteriler    : " + toplam[1].ToString("#,##0.00") + " TL";

            GridDataRefresh();

        }
    }
}
